Event.observe(window, 'load', function() {
	initAdvanceSeo();
});
AdvanceSeos = Class.create();
AdvanceSeos.prototype = {
	textareaElementId: null,
    variablesContent: null,
    dialogWindow: null,
    dialogWindowId: 'variables-chooser',
    overlayShowEffectOptions: null,
    overlayHideEffectOptions: null,
    initialize: function(checkemailUrl,textareaId, objName){  
    	this.checkemailUrl = checkemailUrl;
    	this.textareaId = textareaId;
		this.objName = objName;
    },
    
    button: function () {
        var  response, result;
        response = this.getHttp(this.checkemailUrl);
        //result = response.split(',');        
    },
    
    getHttp: function (url) {
		var variables;
		var count = 0;
		var variablesContent = '<ul>';
        new Ajax.Request(
            url,
            {
                method:       "get",
                onComplete: function (transport) {
                    if (transport.responseText.isJSON()) {
                        //this.init(null, 'advanceseos.insertVariable');
                        variables = transport.responseText.evalJSON();
						count = variables.length;
				
						variablesContent += '<li><b>Select Variable Template</b></li>';
                       	for(var i=0; i<count; i++){
						   if(variables[i].value && variables[i].label){
							   variablesContent += '<li style="padding-left: 20px;">' +
                                	this.prepareVariableRow(variables[i].value, variables[i].label) + '</li>';	
						   }
					   	}
						variablesContent += '</ul>';
						if (variablesContent) {
							this.openDialogWindow(variablesContent);
						}
						
                    }
                }.bind(this)
            });
			
    },
   
	 prepareVariableRow: function(varValue, varLabel) {
        var value = (varValue).replace(/"/g, '&quot;').replace(/'/g, '\\&#39;');
        var content = '<a href="#" onclick="'+this.objName+'.insertVariable(\''+ value +'\');">' + varLabel + '</a>';
        return content;
    },
        openDialogWindow: function(variablesContent) {
			
            if($(this.dialogWindowId) && typeof(Windows) != 'undefined') {
                Windows.focus(this.dialogWindowId);
                return;
            }
		
            this.overlayShowEffectOptions = Windows.overlayShowEffectOptions;
            this.overlayHideEffectOptions = Windows.overlayHideEffectOptions;
            Windows.overlayShowEffectOptions = {duration:0};
            Windows.overlayHideEffectOptions = {duration:0};
			
            this.dialogWindow = Dialog.info(variablesContent, {
                draggable:true,
                resizable:true,
                closable:true,
                className:"magento",
                windowClassName:"popup-window",
                title:'Insert Variable...',
                width:700,
                //height:270,
                zIndex:1000,
                recenterAuto:false,
                hideEffect:Element.hide,
                showEffect:Element.show,
                id:this.dialogWindowId,
                onClose: this.closeDialogWindow.bind(this)
            });
            variablesContent.evalScripts.bind(variablesContent).defer();
        },
        closeDialogWindow: function(window) {
            if (!window) {
                window = this.dialogWindow;
            }
            if (window) {
                window.close();
                Windows.overlayShowEffectOptions = this.overlayShowEffectOptions;
                Windows.overlayHideEffectOptions = this.overlayHideEffectOptions;
            }
        },
		
		insertVariable: function(value) {
			document.getElementById(this.textareaId).value +=  value;
			this.closeDialogWindow(this.dialogWindow);
		  	
    	}
   
}
